<template>
  <el-card class="box-card">
    <el-row type="flex" justify="end" style="margin-bottom: 25px">
      <el-button
        size="small"
        type="success"
        icon="el-icon-edit"
        @click="addPermis"
        >添加菜单</el-button
      >
    </el-row>

    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      :tree-props="{ children: 'childs' }"
      :default-expand-all="true"
      :header-cell-style="{ background: '#fafafa' }"
    >
      <el-table-column prop="title" label="标题" width="220">
        <template slot-scope="{ row }">
          <div style="display: inline-block">
            <span v-if="!row.pid">
              <i class="el-icon-folder-opened"></i>
            </span>

            <span v-else-if="row.childs">
              <i
                v-if="row.childs[0].pid === row.id"
                class="el-icon-document"
              ></i>
            </span>
            <i v-else class="el-icon-view"></i>
            {{ row.title || row.childs.title || row.childs.childs.title }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="code" label="权限点代码" width="800">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{ row }">
          <el-button
            type="primary"
            icon="el-icon-edit"
            circle
            plain
            @click="addPermission(row)"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            circle
            plain
            @click="delPermission(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹层 -->
    <el-dialog
      :title="`${showText}菜单`"
      :visible="showDialog"
      @close="btnCancel"
    >
      <!-- 表单 -->
      <el-form
        ref="addEmployee"
        :model="formData"
        :rules="rules"
        label-width="240px"
      >
        <el-form-item label="权限组名称">
          <el-radio-group v-model="formData.is_point" :disabled="formData.id">
            <el-radio :label="false">菜单</el-radio>
            <el-radio :label="true">权限点</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="权限组名称">
          <el-select
            v-model="formData.pid"
            clearable
            placeholder="请选择"
            ref="selectUpResId"
          >
            <el-option hidden key="id" :value="formData.pid" :label="upResName">
            </el-option>
            <el-tree
              :data="tableData"
              :props="defaultProps"
              :expand-on-click-node="false"
              :check-on-click-node="true"
              default-expand-all
              @node-click="handleNodeClick"
            >
            </el-tree>
          </el-select>
        </el-form-item>

        <el-form-item label="权限代码" prop="code">
          <el-input
            v-model="formData.code"
            style="width: 60%"
            placeholder="请输入用户名"
          />
        </el-form-item>
        <el-form-item label="权限标题" prop="code">
          <el-input
            v-model="formData.title"
            style="width: 60%"
            placeholder="请输入邮箱"
          />
        </el-form-item>
      </el-form>

      <el-row type="flex" justify="center">
        <el-col :span="5">
          <el-row type="flex" justify="space-around">
            <el-button type="primary" @click="btnOK">确定</el-button>
            <el-button @click="btnCancel">取消</el-button>
          </el-row>
        </el-col>
      </el-row>
    </el-dialog>
  </el-card>
</template>

<script>
import { list, add, remove, detail, update } from '@/api/base/menus.js'
export default {
  data () {
    return {
      tableData: [],
      defaultProps: {
        children: 'childs',
        label: 'title'
      },
      showDialog: false,
      formData: {
        pid: null,
        is_point: false,
        code: '',
        title: '',
        id: null
      },
      upResName: '',
      rules: {
        code: [{ required: true, message: '代码不能为空', trigger: 'blur' }],
        title: [{ required: true, message: '标题不能为空', trigger: 'blur' }]
      }
    }
  },

  created () {
    this.load()
  },
  computed: {
    showText () {
      return this.formData.id ? '编辑' : '创建'
    }
  },

  methods: {
    // 获取列表
    async load () {
      const { data } = await list()
      this.tableData = JSON.parse(
        JSON.stringify(data).replace(/"points"/g, '"childs"')
      )
      console.log(this.tableData)
    },
    // 添加
    async addPermis () {
      this.showDialog = true
    },
    // 表单效验
    async btnOK () {
      // 效验表单
      await this.$refs.addEmployee.validate()
      // 提交数据
      // 弹层有两个 需要做判断
      console.log(this.formData)
      if (this.formData.id) {
        // 编辑
        await update(this.formData)
      } else {
        // 新增
        await add(this.formData)
      }
      this.$message.success(`${this.showText}菜单成功`)
      // 重新获取列表数据
      this.load()
      // 关闭
      this.btnCancel()
    },
    // 删除
    delPermission (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await remove(id)
          this.load()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.load()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 编辑
    async addPermission (id) {
      this.showDialog = true
      const res = await detail(id)
      this.formData.id = res.data.id
      this.formData.pid = res.data.pid
      this.formData.is_point = res.data.is_point
      this.formData.code = res.data.code
      this.formData.title = res.data.title

      this.upResName = res.data.title
    },
    // 关
    btnCancel () {
      this.showDialog = false
      this.$refs.addEmployee.resetFields()
      this.formData = {
        pid: null,
        is_point: false,
        code: '',
        title: '',
        id: null
      }
      this.upResName = ''
    },

    // 节点点击事件
    handleNodeClick (data) {
      // 这里主要配置树形组件点击节点后，设置选择器的值；自己配置的数据，仅供参考
      this.upResName = data.title
      this.formData.pid = data.id
      // 选择器执行完成后，使其失去焦点隐藏下拉框的效果
      this.$refs.selectUpResId.blur()
    }
  }
}
</script>

<style scoped lang="less">
.box-card {
  margin: 20px;
  background-color: #fff;
}
</style>
